//package com.jinyu.zhang.aop;
//
//import com.jinyu.zhang.annotation.AuthCheck;
//import com.jinyu.zhang.common.ErrorCode;
//import com.jinyu.zhang.exception.BusinessException;
//import com.jinyu.zhang.model.entity.User;
//import com.jinyu.zhang.model.enums.UserRoleEnum;
//import com.jinyu.zhang.service.UserService;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//
///**
// * 权限校验 AOP
// *
// * @author jinyu
// *
// */
//@Aspect
//@Component
//public class AuthInterceptor {
//
//    @Resource
//    private UserService userService;
//
//    /**
//     * 执行拦截
//     *
//     * @param joinPoint
//     * @param authCheck
//     * @return
//     */
//    @Around("@annotation(authCheck)")
//    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
//        String[] arr = authCheck.mustRole();
//        String mustRole = arr[0];
//        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
//        // 当前登录用户
//        User loginUser = userService.getLoginUser(request);
//        UserRoleEnum mustRoleEnum = UserRoleEnum.getEnumByValue(mustRole);
//        // 不需要权限，放行
//        if (mustRoleEnum == null) {
//            return joinPoint.proceed();
//        }
//        // 必须有该权限才通过
//        UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValue(loginUser.getUserRole());
//        if (userRoleEnum == null) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//        // 如果被封号，直接拒绝
//        if (UserRoleEnum.BAN.equals(userRoleEnum)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//        // 必须有管理员权限
//        if (UserRoleEnum.ADMIN.equals(mustRoleEnum) || UserRoleEnum.LEADER.equals(mustRoleEnum)) {
//            // 用户没有管理员权限，拒绝
//            if (!UserRoleEnum.ADMIN.equals(userRoleEnum) && !UserRoleEnum.LEADER.equals(userRoleEnum)) {
//                throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//            }
//        }
//        return joinPoint.proceed();
//    }
//}
//

package com.jinyu.zhang.aop;

import com.jinyu.zhang.annotation.AuthCheck;
import com.jinyu.zhang.common.ErrorCode;
import com.jinyu.zhang.exception.BusinessException;
import com.jinyu.zhang.model.entity.User;
import com.jinyu.zhang.model.enums.UserRoleEnum;
import com.jinyu.zhang.service.UserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 权限校验 AOP
 *
 * @author jinyu
 */
@Aspect
@Component
public class AuthInterceptor {

    @Resource
    private UserService userService;

    /**
     * 执行拦截
     *
     * @param joinPoint
     * @param authCheck
     * @return
     */
    @Around("@annotation(authCheck)")
    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
        // 获取注解中指定的必须角色数组
        String[] mustRoles = authCheck.mustRole();
        // 获取当前请求
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);
        // 获取当前用户的角色枚举
        UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValue(loginUser.getUserRole());

        // 如果用户未登录或角色为空，直接拒绝
        if (loginUser == null || userRoleEnum == null) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }

        // 如果用户被封号，直接拒绝
        if (UserRoleEnum.BAN.equals(userRoleEnum)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }

        // 如果没有指定必须角色，放行
        if (mustRoles.length == 0) {
            return joinPoint.proceed();
        }

        // 检查用户是否有权限访问
        boolean hasPermission = false;
        for (String mustRole : mustRoles) {
            UserRoleEnum mustRoleEnum = UserRoleEnum.getEnumByValue(mustRole);
            if (mustRoleEnum == null) {
                continue; // 跳过无效的角色
            }
            // 如果用户角色是ADMIN或LEADER，可以直接访问
            if (UserRoleEnum.ADMIN.equals(userRoleEnum) || UserRoleEnum.LEADER.equals(userRoleEnum)) {
                hasPermission = true;
                break;
            }
            // 如果用户角色是USER，且接口没有指定必须是ADMIN或LEADER，也可以访问
            if (UserRoleEnum.USER.equals(userRoleEnum) && !UserRoleEnum.ADMIN.equals(mustRoleEnum) && !UserRoleEnum.LEADER.equals(mustRoleEnum)) {
                hasPermission = true;
                break;
            }
        }

        // 如果没有权限，抛出异常
        if (!hasPermission) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }

        // 通过权限校验，放行
        return joinPoint.proceed();
    }
}
